package com.pqc.tbk.utils;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * 
 * @author 赵龙, create on 2012-6-2 下午5:10:36
 * 
 */
public class JqGridSearchTo {

	private String filters; // 多字段查询的时候接受的参数
	private String groupOp; // 多字段查询时分组类型，主要是AND或者OR
	private List rules; // 多字段查询时候，查询条件的集合

	private int page; // 当前第几页
	private int rows; // 每页显示多少条数据
	private String sidx; // 排序字段
	private String sord; // 排序类型 ASC或者DESC

	private String searchField; // 单字段查询的时候，查询字段名称
	private String searchString; // 单字段查询的时候，查询字段的值
	private String searchOper; // 单字段查询的时候，查询的操作

	/**
	 * 单条件查询加上排序
	 * 
	 * @param condition
	 *            要插入的条件 格式 :and a='1' ...
	 * @return
	 */
	public String getSingleOperation(final String condition) {
		final StringBuilder result = new StringBuilder(
				singleConsultOperation(condition));
		result.append(getOrderString());
		return result.toString();
	}

	/**
	 * 单条件查询 不加排序条件
	 * 
	 * @param condition
	 *            格式 and a='1' ...
	 * @return
	 */
	public String singleConsultOperation(final String condition) {
		final StringBuilder result = new StringBuilder(" ");
		// 获取查询条件
		result.append(getOperation(searchField, searchOper, searchString));
		// 判断搜出的条件是否为空 不为空的话再加上and
		if (!result.toString().equals("")) {
			result.insert(0, " and ");
		}
		// 获取排序的字符串
		result.append(condition);
		return result.toString();
	}

	// ---------------------------------------------------------------

	/**
	 * 多条件查询加上排序字符串<br>
	 * 注意json转化出来的为linkedHashMap对象<br>
	 * 
	 * @param condition
	 * @return
	 */
	public String getMutiOperation(final String condition) {
		final StringBuilder result = new StringBuilder(
				mutiConsultOperation(condition));
		result.append(getOrderString());
		return result.toString();
	}

	/**
	 * 多条件查询不加上排序字符串<br>
	 * 
	 * @param condition
	 *            条件的格式为:and a='3' ...
	 * @return
	 */
	public String mutiConsultOperation(final String condition) {
		final StringBuilder result = new StringBuilder("");
		for (int i = 0; i < rules.size(); i++) {
			@SuppressWarnings("unchecked")
			final LinkedHashMap<String, String> map = (LinkedHashMap<String, String>) rules
					.get(i);
			final String searchField = map.get("field");
			final String searchOper = map.get("op");
			final String searchString = map.get("data");
			result.append(getOperation(searchField, searchOper, searchString));
			if (i < (rules.size() - 1)) {
				result.append(" " + groupOp + " ");
			}

		}
		// 判断搜出的条件是否为空 不为空的话再加上and
		if (!result.toString().equals("")) {
			result.insert(0, " and ");
		}
		result.append(condition);
		return result.toString();
	}

	// ----------------------------------------------------------------------------------
	/**
	 * 将传递过来的参数拼接成搜索的条件
	 * 
	 * @param searchField
	 *            搜索的字段
	 * @param searchOper
	 *            搜索的操作
	 * @param searchString
	 *            搜索的值
	 * @return
	 */
	public String getOperation(final String searchField,
			final String searchOper, final String searchString) {
		String result = "";
		if ((searchString == null) || (searchString.trim().length() == 0)) {
			result = searchField + " is  null ";
		} else {
			if (searchOper.trim().equals("eq")) { // 等于
				result = searchField + "='" + searchString + "' ";
			} else if (searchOper.trim().equals("gt")) { // 大于
				result = searchField + " > '" + searchString + "' ";
			} else if (searchOper.trim().equals("lt")) { // 小于
				result = searchField + " < '" + searchString + "' ";
			} else if (searchOper.trim().equals("cn")) { // 包含
				result = searchField + " LIKE'%" + searchString + "%' ";
			} else if (searchOper.trim().equals("nc")) { // 不包含
				result = searchField + "NOT LIKE'%" + searchString + "%' ";
			} else if (searchOper.trim().equals("ne")) {// 不等于
				result = searchField + " != '" + searchString + "' ";
			} else if (searchOper.trim().equals("le")) {// 小于等于
				result = searchField + " <= '" + searchString + "' ";
			} else if (searchOper.trim().equals("ge")) {// 大于等于
				result = searchField + " >= '" + searchString + "' ";
			} else if (searchOper.trim().equals("bw")) {// 开始于
				result = searchField + " LIKE '" + searchString + "%' ";
			} else if (searchOper.trim().equals("bn")) {// 不开始于
				result = searchField + " NOT LIKE '" + searchString + "%' ";
			}
		}
		return result;
	}

	/**
	 * 获取排序的字符串
	 * 
	 * @return
	 */
	public String getOrderString() {
		return " order by " + sidx + " " + sord;
	}

	// 构造器
	public JqGridSearchTo() {
		super();
	}

	// 构造器
	public JqGridSearchTo(final String groupOp, final List rules,
			final int page, final int rows, final String sidx,
			final String sord, final String searchField,
			final String searchString, final String searchOper) {
		super();
		this.groupOp = groupOp;
		this.rules = rules;
		this.page = page;
		this.rows = rows;
		this.sidx = sidx;
		this.sord = sord;
		this.searchField = searchField;
		this.searchString = searchString;
		this.searchOper = searchOper;
	}

	public String getGroupOp() {
		return groupOp;
	}

	public void setGroupOp(final String groupOp) {
		this.groupOp = groupOp;
	}

	public List getRules() {
		return rules;
	}

	public void setRules(final List rules) {
		this.rules = rules;
	}

	public int getPage() {
		return page;
	}

	public void setPage(final int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(final int rows) {
		this.rows = rows;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(final String sidx) {
		this.sidx = sidx;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(final String sord) {
		this.sord = sord;
	}

	public String getSearchField() {
		return searchField;
	}

	public void setSearchField(final String searchField) {
		this.searchField = searchField;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(final String searchString) {
		this.searchString = searchString;
	}

	public String getSearchOper() {
		return searchOper;
	}

	public void setSearchOper(final String searchOper) {
		this.searchOper = searchOper;
	}

	public String getFilters() {
		return filters;
	}

	public void setFilters(final String filters) {
		this.filters = filters;
	}

	@Override
	public String toString() {
		final StringBuilder stringBuilder = new StringBuilder(groupOp + ",");
		for (int i = 0; i < rules.size(); i++) {
			stringBuilder.append(rules.get(i).toString());
		}
		return stringBuilder.toString();
	}

}
